import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:gluz/app/data/area_entity.dart';
import 'package:gluz/app/modules/auth/views/auth_base_view.dart';
import 'package:gluz/app_session.dart';

import '../../../../common/common_button.dart';
import '../../../../common/form_components.dart';
import '../controllers/auth_hdgs_controller.dart';

class AuthHdgsView extends GetView<AuthHdgsController> with AuthBaseView {
  const AuthHdgsView({super.key});

  @override
  Widget build(BuildContext context) {
    return super.buildWidget();
  }

  // 表单内容
  @override
  Widget buildFormContent() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 15),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withAlpha(50),
            blurRadius: 5,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Padding(
        padding: const EdgeInsets.all(20),
        child: GetBuilder<AuthHdgsController>(
            builder: (controller) {
              return Stack(
                children: [
                  Obx(() {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        // 用户头像
                        Obx(
                              () =>
                              AvatarUploadField(
                                label: '用户头像'.tr,
                                imagePath: controller.avatarPath.value,
                                defaultAssets: 'assets/images/auth/auth_upload_image.png',
                                onTap: controller.uploadAvatar,
                              ),
                        ),
                        //认证类型
                        Obx(
                              () =>
                              FormRadioGroup(
                                label: '认证类型'.tr,
                                isRequired: true,
                                options: ['个人'.tr, '公司'.tr],
                                selectedValue: controller.selectedAuthType
                                    .value,
                                onChanged: controller.updateAuthType,
                              ),
                        ),

                        if (controller.selectedAuthType.value == '个人'.tr) ...[
                          FormInputField(
                            label: '用户名称'.tr,
                            isRequired: true,
                            controller: controller.userNickPhoneController,
                          ),
                          FormInputField(
                            label: '手机号'.tr,
                            readOnly: true,
                            isRequired: true,
                            keyboardType: TextInputType.number,
                            controller: controller.userPhoneController,
                          ),
                          FormInputField(
                            label: '姓名'.tr,
                            isRequired: true,
                            controller: controller.userNameController,
                          ),
                          // 企业名称
                          FormInputField(
                            label: '身份证号'.tr,
                            isRequired: true,
                            keyboardType: TextInputType.number,
                            controller: controller.userIdNumController,
                          ),
                          // 身份证照片
                          Obx(
                                () =>
                                DualImageUploadField(
                                  label: '身份证照片'.tr,
                                  frontImagePath: controller.idCardFrontPath
                                      .value,
                                  backImagePath: controller.idCardBackPath
                                      .value,
                                  onFrontTap: controller.uploadIdCardFront,
                                  onBackTap: controller.uploadIdCardBack,
                                ),
                          ),
                        ],
                        if (controller.selectedAuthType.value == '公司'.tr) ...[
                          Padding(
                            padding: const EdgeInsets.symmetric(vertical: 10),
                            child: Text(
                              '企业信息'.tr,
                              style: TextStyle(
                                  fontSize: 17, fontWeight: FontWeight.bold),
                            ),
                          ),

                          // 企业名称
                          FormInputField(
                            label: '企业名称'.tr,
                            isRequired: true,
                            controller: controller.enterpriseNameController,
                          ),
                          // // 统一社会信用代码
                          FormInputField(
                            label: '统一社会信用代码'.tr,
                            isRequired: true,
                            controller: controller.creditCodeController,
                          ),
                          // 营业执照
                          ImageUploadField(
                            label: '营业执照'.tr,
                            isRequired: true,
                            placeholderText: '点击上传营业执照'.tr,
                            imagePath: controller.businessLicensePath.value,
                            onTap: controller.uploadBusinessLicense,
                          ),

                          if (!app.isLangRU())
                            ...[
                              // 公司照片和视频上传
                              Obx(() {
                                return ImageUploadFieldCircle(
                                  label: '公司照片'.tr,
                                  imageTip: '上传照片'.tr,
                                  defaultAssets: 'assets/images/auth/auth_upload_image.png',
                                  imagePath: controller.companyPhotoPath.value,
                                  onTap: controller.uploadCompanyPhoto,
                                  onDelete: controller.onTapCompanyPhotoDelete,
                                );
                              }),
                              Obx(() {
                                return ImageUploadFieldCircle(
                                  label: '公司视频'.tr,
                                  imageTip: '上传视频'.tr,
                                  defaultAssets: 'assets/images/auth/auth_upload_video.png',
                                  imagePath: controller.companyVideoPathFrame,
                                  onTap: controller.uploadCompanyVideo,
                                  onDelete: controller.onTapCompanyVideoDelete,
                                );
                              }),

                              // 公司介绍
                              FormInputField(
                                label: '公司介绍'.tr,
                                isRequired: false,
                                controller: controller.introduceController,
                              ),

                              Padding(
                                padding: const EdgeInsets.symmetric(vertical: 10),
                                child: Text(
                                  '法人信息'.tr,
                                  style: TextStyle(
                                      fontSize: 17, fontWeight: FontWeight.bold),
                                ),
                              ),
                              // 法定代表人姓名
                              FormInputField(
                                label: '法定代表人姓名'.tr,
                                isRequired: true,
                                controller: controller
                                    .legalRepresentativeNameController,
                              ),
                              // 法定代表人身份证号
                              FormInputField(
                                label: '法定代表人身份证号'.tr,
                                isRequired: true,
                                controller: controller
                                    .legalRepresentativeIdController,
                                placeholder: '请输入'.tr,
                              ),
                              // 身份证照片
                              Obx(
                                    () =>
                                    DualImageUploadField(
                                      label: '身份证照片'.tr,
                                      frontImagePath: controller
                                          .legalIdCardFrontPath
                                          .value,
                                      backImagePath: controller.legalIdCardBackPath
                                          .value,
                                      onFrontTap: controller.uploadLegalIdCardFront,
                                      onBackTap: controller.uploadLegalIdCardBack,
                                    ),
                              ),

                              // 代理人信息
                              Padding(
                                padding: const EdgeInsets.symmetric(vertical: 10),
                                child: Text(
                                  '代理人信息'.tr,
                                  style: TextStyle(
                                      fontSize: 17, fontWeight: FontWeight.bold),
                                ),
                              ),

                              // 管理人与法人是否同一人
                              Obx(
                                    () =>
                                    FormRadioGroup(
                                      label: '管理人与法人是否同一人'.tr,
                                      isRequired: true,
                                      options: ['是'.tr, '否'.tr],
                                      selectedValue: controller.isManagerSameAsLegal
                                          .value,
                                      onChanged: controller
                                          .updateManagerSameAsLegal,
                                    ),
                              ),

                              if (controller.isManagerSameAsLegal.value == '否'.tr)
                              // 管理人身份证照片
                                Obx(
                                      () =>
                                      DualImageUploadField(
                                        label: '管理人身份证照片'.tr,
                                        frontImagePath: controller.idCardFrontPath
                                            .value,
                                        backImagePath: controller.idCardBackPath
                                            .value,
                                        onFrontTap: controller.uploadIdCardFront,
                                        onBackTap: controller.uploadIdCardBack,
                                      ),
                                ),
                            ],
                        ],

                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 10),
                          child: Text(
                            '路线信息'.tr,
                            style: TextStyle(
                                fontSize: 17, fontWeight: FontWeight.bold),
                          ),
                        ),

                        // 主营线路
                        RoadMapWidget(
                          roadMapList: controller.roadMapList,
                          onTapAdd: controller.addRoadMap,
                        ),

                        TagSelectorField(
                          label: '设置服务项目'.tr,
                          isRequired: true,
                          options: controller.serverItemList,
                          selectedOptions: controller.serverItemSelectedList,
                          onTagTap: controller.updateServerItem,
                        ),

                        FormInputField(
                          label: '其他服务项目描述'.tr,
                          isRequired: false,
                          controller: controller.otherServerDescController,
                        ),

                        Obx(() {
                          return ImageUploadFieldCircle(
                            label: '收费标准'.tr,
                            imageTip: '上传照片'.tr,
                            defaultAssets: 'assets/images/auth/auth_upload_image.png',
                            imagePath: controller.shoufeibiaozhun.value,
                            onTap: controller.onTapShoufeibiaozhun,
                            onDelete: controller.onTapShoufeibiaozhunDelete,
                          );
                        }),

                        FormInputField(
                          label: '客服保障服务描述'.tr,
                          isRequired: false,
                          controller: controller.serverDescController,
                        ),
                      ],
                    );
                  }),
                  controller.getStatusWidget(),
                ],
              );
            }
        ),
      ),
    );
  }

  // 提交按钮
  @override
  Widget buildSubmitBtn() {
    // 提交按钮
    return SafeArea(
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
        child: GLGradientButton(
          onPressed: controller.submitForm,
          child: Text('确定'.tr, style: TextStyle(color: Colors.white)),
        ),
      ),
    );
  }
}
